/*
 * @Author: yangzonglong
 * @Date: 2021-05-08 11:12:39
 * @version: v1.0.0
 * @Descripttion: form api
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-07-16 14:12:55
 * @Auditor: 
 */
import { units } from '@library';
import dayjs from 'dayjs';
import { FormProps } from './../../interface';

export async function saveForm(data: FormProps) {
  try {
    if (!data.categoryCode) data.categoryCode = 'workflow';

    const action = data.id ? 'update' : 'add';
    const result = await units.serverLoading(`/bk/api/fd/Form/${action}`, { data });
    return result;
  } catch (error) {
    return false;
  }
}


export async function removeForm(id: string) {
  try {
    units.serverLoading(`/bk/api/fd/Form/delete`, { data: { id } });
    return true;
  } catch (error) {
    return false;
  }
}

export async function getForm(id: string, attributes?: string[]): Promise<FormProps | boolean> {
  try {
    const result: FormProps = await units.serverLoading(`/bk/api/fd/Form/findOne`, { where: { id }, attributes: attributes ? attributes : undefined });
    result.createdAt = dayjs(result.createdAt).format(window.$app.MOMENT_FORMAT_TIME);
    return result;
  } catch (error) {
    return false;
  }
}